package com.scottmcfurry.psx;

import java.io.File;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.net.Uri;
import android.os.Bundle;
import android.preference.Preference;
import android.preference.PreferenceActivity;
import android.preference.PreferenceGroup;
import android.preference.PreferenceManager;
import android.preference.PreferenceScreen;

import com.androidemu.Emulator;
import com.scottmcfurry.psx.wrapper.Wrapper;

public class EmulatorSettings extends PreferenceActivity {

//	private static final String SEARCH_ROM_URI =
//			"http://www.romfind.com/snes-roms.html?sid=YONG";
//	private static final Uri ABOUT_URI = Uri.parse(
//			"file:///android_asset/about.html");
	private static final String MARKET_SEARCH_URI = "market://details?id=";//
	private static final String GAME_GRIPPER_URI = 
			"https://sites.google.com/site/gamegripper";//
	
	private static final int REQUEST_BIOS_FILE = 1;//
//	private static final int REQUEST_LOAD_KEY_PROFILE = 1;
//	private static final int REQUEST_SAVE_KEY_PROFILE = 2;
	private SharedPreferences settings;//

	public static final int[] gameKeys = {
		Emulator.GAMEPAD_UP,
		Emulator.GAMEPAD_DOWN,
		Emulator.GAMEPAD_LEFT,
		Emulator.GAMEPAD_RIGHT,
		Emulator.GAMEPAD_UP_LEFT,
		Emulator.GAMEPAD_UP_RIGHT,
		Emulator.GAMEPAD_DOWN_LEFT,
		Emulator.GAMEPAD_DOWN_RIGHT,
		Emulator.GAMEPAD_SELECT,
		Emulator.GAMEPAD_START,
		Emulator.GAMEPAD_CROSS,
		Emulator.GAMEPAD_TRIANGLE,
		Emulator.GAMEPAD_SQUARE,
		Emulator.GAMEPAD_CIRCLE,
		Emulator.GAMEPAD_TL,
		Emulator.GAMEPAD_TR,
		Emulator.GAMEPAD_TL2,
		Emulator.GAMEPAD_TR2
	};

	public static final String[] gameKeysPref = {
		"gamepad_up",
		"gamepad_down",
		"gamepad_left",
		"gamepad_right",
		"gamepad_up_left",
		"gamepad_up_right",
		"gamepad_down_left",
		"gamepad_down_right",
		"gamepad_select",
		"gamepad_start",
		"gamepad_cross",
		"gamepad_triangle",
		"gamepad_square",
		"gamepad_circle",
		"gamepad_TL",
		"gamepad_TR",
		"gamepad_TL2",
		"gamepad_TR2"
	};

	private static final int[] gameKeysName = {
		R.string.gamepad_up,
		R.string.gamepad_down,
		R.string.gamepad_left,
		R.string.gamepad_right,
		R.string.gamepad_up_left,
		R.string.gamepad_up_right,
		R.string.gamepad_down_left,
		R.string.gamepad_down_right,
		R.string.gamepad_select,
		R.string.gamepad_start,
		R.string.gamepad_cross,
		R.string.gamepad_triangle,
		R.string.gamepad_square,
		R.string.gamepad_circle,
		R.string.gamepad_TL,
		R.string.gamepad_TR,
		R.string.gamepad_TL2,
		R.string.gamepad_TR2
	};

	static {
		final int n = gameKeys.length;
		if (gameKeysPref.length != n ||
				gameKeysName.length != n)
			throw new AssertionError("Key configurations are not consistent");
	}

	public static Intent getSearchROMIntent() {
		return new Intent(Intent.ACTION_WEB_SEARCH).putExtra("query", "psx+roms").setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		setTitle(R.string.settings);
		addPreferencesFromResource(R.xml.preferences);

		settings = PreferenceManager.getDefaultSharedPreferences(this);

		final int[] defaultKeys = DefaultPreferences.getKeyMappings(this);

		// gamepad 1
		PreferenceGroup group = (PreferenceGroup) findPreference("keyMappings");
		for (int i = 0; i < gameKeysPref.length; i++) {
			KeyPreference pref = new KeyPreference(this);
			pref.setKey(gameKeysPref[i]);
			pref.setTitle(gameKeysName[i]);
			pref.setDefaultValue(defaultKeys[i]);
			group.addPreference(pref);
		}
		
		if (!Wrapper.supportsMultitouch(this)) {
			findPreference("enableVKeypad").setSummary(R.string.multitouch_unsupported);
		}
		
		Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(MARKET_SEARCH_URI+getPackageName()));
		findPreference("appAbout").setIntent(intent);
		findPreference("searchRoms").setIntent(EmulatorSettings.getSearchROMIntent());
		findPreference("gameGripper").setIntent(new Intent(Intent.ACTION_VIEW, Uri.parse(GAME_GRIPPER_URI)));
		String bios = settings.getString("biosFile", null);
		if (bios!=null) {
			findPreference("biosFile").setSummary(bios);
		}
	}

	@Override
	protected void onActivityResult(int request, int result, Intent data) {
		switch (request) {
		case REQUEST_BIOS_FILE:
			if (result == RESULT_OK) {
				String  bios = data.getData().getPath();
				settings.edit().putString("biosFile", bios).commit();
				findPreference("biosFile").setSummary(bios);
			}
		}
	}

	@Override
	public boolean onPreferenceTreeClick(PreferenceScreen screen,
			Preference pref) {

		if ("biosFile".equals(pref.getKey())) {
			String bios = settings.getString("biosFile", null);
			Uri uri = null;
			if (bios!=null) {
				uri = Uri.fromFile(new File(bios));
			}
			
			
			Intent intent = new Intent(this, FileChooser.class);
			intent.setData(uri);
			intent.putExtra("title", getResources().getString(R.string.title_select_bios));
			intent.putExtra("filters", new String[]{".bin"});
			startActivityForResult(intent, Activity.RESULT_FIRST_USER);
			return true;
		}
		return super.onPreferenceTreeClick(screen, pref);
	}
}
